package com.cloudpivot.receivales.crm.service;


import com.cloudpivot.receivales.crm.entity.contract.ContractEntity;
import com.cloudpivot.receivales.crm.entity.weekly.WeeklyCount;
import com.cloudpivot.receivales.crm.repository.contract.ContractCalRepository;
import com.cloudpivot.receivales.crm.repository.weekly.WeeklyCountRepository;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 合同计算服务
 */
@Service
@Slf4j
public class ContractCalServiceImpl {

    @Autowired
    private ContractCalRepository contractCalRepository;

    @Autowired
    private ObjectMapper objectMapper;

    public void getList() {
        //属于 正常的列表
        List<ContractEntity> normalLists=contractCalRepository.getNormalList();
        //属于内部合同的列表
        List<ContractEntity> innerLists=contractCalRepository.getInnerList();

        //产品组1==
        Map<String, Object> map1 = new HashMap<>();
        //产品组2==
        Map<String, Object> map2 = new HashMap<>();
        List<String> list1 = new ArrayList<>();

        //比较:内部合同中 哪些是在正式合同中 重复出现的产品
        for (ContractEntity entity : innerLists) {
            Map<String,Object> _map = viewInner(normalLists, entity.getNew_ccusname(), entity.getNew_cinvcode(), entity.getNew_iquantity());
            if(Objects.isNull(_map)){
                //有出现重复
                map1.put(entity.getNew_sub_contractid(), entity.getNew_cinvcode());
                list1.add("内部合同" + entity.getNew_name() + "与正式类型合同(" + map1.get("new_name").toString() + ")发生重复,发生重复的产品为:" + entity.getNew_cinvcode());
            }
        }
        //结果打印
        printRet(list1);

    }

    /**
     * @param list 正常的合同 list合集
     */
    private Map<String,Object> viewInner(List<ContractEntity> list, String _cusName,String _code,Double _inatsum) {
        for (ContractEntity e : list) {
            Map<String, Object> map = new HashMap<>();
            String cusName=e.getNew_ccusname();
            String code = e.getNew_cinvcode();
            Double new_inatsum = e.getNew_inatsum();
            if (cusName.equals(_cusName) && code.equals(_code) && new_inatsum==_inatsum) {
                //有相同的内容(考虑是否需要追加)
                map.put("flag", true);
                map.put("new_name",e.getNew_name());
                return map;
            }
        }
        return null;
    }

    private void printRet(List<String> list) {
        //结果打印
        for (String s : list) {
            System.out.println(s);
        }
    }
}
